#include "dbf_file.h"
#include "open_file.h"
#include "charsetconverter.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
	open_file of("show2003.dbf");
	char *buf = of.get_file_buffer();
	dbf_reader dr(buf, of.get_file_size());

	cout << "head size " << dr.get_head_size() << endl;
	cout << "get time " << dr.get_update() << endl;
	cout << "record count " << dr.get_record_count() << endl;
	cout << "record size " << dr.get_record_size() << endl;
	cout << "field count " << dr.get_field_count() << endl;

	int s1_index = dr.get_field_index_by_name("s1");
	int s2_index = dr.get_field_index_by_name("s2");
	int s3_index = dr.get_field_index_by_name("s3");
	int s4_index = dr.get_field_index_by_name("s4");
	int s5_index = dr.get_field_index_by_name("s5");
	int s6_index = dr.get_field_index_by_name("s6");
	int s7_index = dr.get_field_index_by_name("s7");
	int s8_index = dr.get_field_index_by_name("s8");
	int s9_index = dr.get_field_index_by_name("s9");
	int s10_index = dr.get_field_index_by_name("s10");
	int s11_index = dr.get_field_index_by_name("s11");
	int s13_index = dr.get_field_index_by_name("s13");
	int s15_index = dr.get_field_index_by_name("s15");
	int s16_index = dr.get_field_index_by_name("s16");
	int s17_index = dr.get_field_index_by_name("s17");
	int s18_index = dr.get_field_index_by_name("s18");
	int s19_index = dr.get_field_index_by_name("s19");
	int s21_index = dr.get_field_index_by_name("s21");
	int s22_index = dr.get_field_index_by_name("s22");
	int s23_index = dr.get_field_index_by_name("s23");
	int s24_index = dr.get_field_index_by_name("s24");
	int s25_index = dr.get_field_index_by_name("s25");
	int s26_index = dr.get_field_index_by_name("s26");
	int s27_index = dr.get_field_index_by_name("s27");
	int s28_index = dr.get_field_index_by_name("s28");
	int s29_index = dr.get_field_index_by_name("s29");
	int s30_index = dr.get_field_index_by_name("s30");
	int s31_index = dr.get_field_index_by_name("s31");
	int s32_index = dr.get_field_index_by_name("s32");
	int s33_index = dr.get_field_index_by_name("s33");

	vector<stock_quote> qutoes;
	stock_quote q;
	memset((void *)&q, 0, sizeof(stock_quote));
	dr.get_field_value(0, s1_index, q.code);
	dr.get_field_value(0, s2_index, q.time);
	dr.get_field_value(0, s6_index, q.date);

	dr.get_field_value(0, s3_index, q.high);
	dr.get_field_value(0, s4_index, q.low);
	dr.get_field_value(0, s13_index, q.now);
	q.mt = 1;
	qutoes.push_back(q);

	int time = q.time;
	int date = q.date;
	cout << "code " << q.code << endl;
	cout << "time " << q.time << endl;
	cout << "date " << q.date << endl;
	cout << "A    " << q.high << endl;
	cout << "B    " << q.low << endl;
	cout << "update " << q.now << endl;

	charsetconverter cc("utf-8", "gb2312");
	for (unsigned int i = 1; i < dr.get_record_count(); i++)
	{
		stock_quote sq;
		memset((void *)&sq, 0, sizeof(stock_quote));

		sq.mt = 1;
		sq.time = time;
		sq.date = date;
		dr.get_field_value(i, s1_index, sq.code);
		char namebuf[9];
		memset(namebuf, 0, 9);
		dr.get_field_value(i, s2_index, namebuf);
		size_t inlen = strlen(namebuf);
		size_t outlen = sizeof(sq.name) - 1;
		cc.convert(namebuf, inlen, sq.name, outlen);
		dr.get_field_value(i, s3_index, sq.last_close);
		dr.get_field_value(i, s4_index, sq.open);
		dr.get_field_value(i, s5_index, sq.amount);
		dr.get_field_value(i, s6_index, sq.high);
		dr.get_field_value(i, s7_index, sq.low);
		dr.get_field_value(i, s8_index, sq.now);
		dr.get_field_value(i, s11_index, sq.volume);

		dr.get_field_value(i, s9_index, sq.ask1);
		dr.get_field_value(i, s10_index, sq.bid1);
		dr.get_field_value(i, s13_index, sq.rate);
		dr.get_field_value(i, s15_index, sq.ask1_vol);
		dr.get_field_value(i, s16_index, sq.ask2);
		dr.get_field_value(i, s17_index, sq.ask2_vol);
		dr.get_field_value(i, s18_index, sq.ask3);
		dr.get_field_value(i, s19_index, sq.ask3_vol);
		dr.get_field_value(i, s21_index, sq.bid1_vol);
		dr.get_field_value(i, s22_index, sq.bid2);
		dr.get_field_value(i, s23_index, sq.bid2_vol);
		dr.get_field_value(i, s24_index, sq.bid3);
		dr.get_field_value(i, s25_index, sq.bid3_vol);
		dr.get_field_value(i, s26_index, sq.ask4);
		dr.get_field_value(i, s27_index, sq.ask4_vol);
		dr.get_field_value(i, s28_index, sq.ask5);
		dr.get_field_value(i, s29_index, sq.ask5_vol);
		dr.get_field_value(i, s30_index, sq.bid4);
		dr.get_field_value(i, s31_index, sq.bid4_vol);
		dr.get_field_value(i, s32_index, sq.bid5);
		dr.get_field_value(i, s33_index, sq.bid5_vol);
		qutoes.push_back(sq);

		cout << "code: " << sq.code << endl;
		cout << "name: " << sq.name << endl;
		cout << "close: " << sq.last_close << endl;
		cout << "amount: " << sq.amount << endl;
		cout << "high: " << sq.high << endl;
		cout << "low: " << sq.low << endl;
		cout << "now: " << sq.now << endl;
		cout << "volume: " << sq.volume << endl;
		cout << "rate: " << sq.rate << endl;
		cout << "bid1: " << sq.bid1 << endl;
		cout << "bid1: " << sq.bid1_vol << endl;
		cout << "bid2: " << sq.bid2 << endl;
		cout << "bid2: " << sq.bid2_vol << endl;
		cout << "bid3: " << sq.bid3 << endl;
		cout << "bid3: " << sq.bid3_vol << endl;
		cout << "bid4: " << sq.bid4 << endl;
		cout << "bid4: " << sq.bid4_vol << endl;
		cout << "bid5: " << sq.bid5 << endl;
		cout << "bid5: " << sq.bid5_vol << endl;
		cout << "ask1: " << sq.ask1 << endl;
		cout << "ask1: " << sq.ask1_vol << endl;
		cout << "ask2: " << sq.ask2 << endl;
		cout << "ask2: " << sq.ask2_vol << endl;
		cout << "ask3: " << sq.ask3 << endl;
		cout << "ask3: " << sq.ask3_vol << endl;
		cout << "ask4: " << sq.ask4 << endl;
		cout << "ask4: " << sq.ask4_vol << endl;
		cout << "ask5: " << sq.ask5 << endl;
		cout << "ask5: " << sq.ask5_vol << endl;
		cout << "\n";

	}

	return 0;
}

